package algorithms.leecode;

import java.util.ArrayList;
import java.util.List;

public class Au54 {
    public static void main(String[] args) {
        int[][]mat = {
                {1,2},


        };
        System.err.println(spiralOrder(mat));

    }
    public static List<Integer> spiralOrder(int[][] matrix) {
        int horiLen = matrix[0].length - 1;
        int vertLen = matrix.length - 1;
        int mx = Math.min(horiLen + 1,vertLen + 1);
        int count = 2 * mx - 1;
        List<Integer> list = new ArrayList<>();
        int startIndex = 0;
        int endIndex = horiLen;
        for (int i = startIndex; i <= endIndex; i++) {
            list.add(matrix[0][i]);
        }
        downMove(matrix,list,endIndex,0,horiLen,vertLen,count,1);
        return list;
    }
    public static void rightMove(int[][] matrix,List<Integer> list,int indexX,int indexY,int horiMaxIndex,int verMaxIndex,int count,int curCOunt) {

        int startIndex = indexX + 1;
        int endIndex = horiMaxIndex - startIndex;
        for (int i = startIndex; i <= endIndex; i++) {
            list.add(matrix[indexY][i]);
        }
        if (count == curCOunt) {
            return;
        }
        downMove(matrix,list,endIndex,indexY,horiMaxIndex,verMaxIndex,count,++curCOunt);
    }
    public static void downMove(int[][] matrix,List<Integer> list,int indexX,int indexY,int horiMaxIndex,int verMaxIndex,int count,int curCount) {

        int startIndex = indexY + 1;
        int endIndex = verMaxIndex - indexY;
        for (int i = startIndex; i <= endIndex; i++) {
            list.add(matrix[i][indexX]);
        }
        if (count == curCount) {
            return;
        }
        leftMove(matrix,list,indexX,endIndex,horiMaxIndex,verMaxIndex,count,++curCount);
    }
    public static void leftMove(int[][] matrix,List<Integer> list,int indexX,int indexY,int horiMaxIndex,int verMaxIndex,int count,int curCount) {

        int startIndex = indexX - 1;
        int endIndex = horiMaxIndex - indexX;
        for (int i = startIndex; i >= endIndex; i--) {
            list.add(matrix[indexY][i]);
        }
        if (count == curCount) {
            return;
        }
        upMove(matrix,list,endIndex,indexY,horiMaxIndex,verMaxIndex,count,++curCount);
    }
    public static void upMove(int[][] matrix,List<Integer> list,int indexX,int indexY,int horiMaxIndex,int verMaxIndex,int count,int curCount) {

        int startIndex = indexY - 1;
        int endIndex = verMaxIndex - startIndex;
        for (int i = startIndex; i >= endIndex; i--) {
            list.add(matrix[i][indexX]);
        }
        if (count == curCount) {
            return;
        }
        rightMove(matrix,list,indexX,endIndex,horiMaxIndex,verMaxIndex,count,++curCount);
    }
}
